/*
 * @Author: cmd 
 * @wechat: cmd_develop 
 * @qq群: 790373986
 * @Date: 2019-04-23 15:33:22 
 * @Last Modified by: cmd
 * @Last Modified time: 2019-04-23 17:14:25
 * @Description: 
 */

const {ccclass, property} = cc._decorator;

@ccclass
export default class NewClass extends cc.Component {
    public static async http_post(url:string,params:any) {
        return new Promise((resolve,reject)=>{           
            var xhr = new XMLHttpRequest();
            xhr.timeout = 5000;// 5 seconds for timeout
            xhr.onreadystatechange = function () {
                if (xhr.readyState === 4 && (xhr.status >= 200 && xhr.status < 300)) {
                    var respone = xhr.responseText; 
                    resolve(JSON.parse(respone));
                }                          
            };
            let url_temp:string;
            if(orc.orc_config.DEBUG == true)
            {
                url_temp= orc.orc_config.HTTP_TEST_URL + url;
            }
            else
            {
                url_temp= orc.orc_config.HTTP_ONLINE_URL + url;
            }            
            xhr.open("POST", url_temp, true);           
           
            params['token'] = orc.orc_data.memory_data.token;
            let form = new FormData();   
            for(let key in params)
            {
                form.append(key,params[key])
            }
            
            xhr.send(form);
        })        
    }

    public static async http_get(url:string,loop:boolean,params:any) {
        return new Promise((resolve,reject)=>{
            if(loop == false)
            {
                var xhr = cc.loader.getXMLHttpRequest();

                // xhr.withCredentials = true;

                var time = false;//是否超时
                var timer = setTimeout(function(){
                    time = true;
                    xhr.abort();//请求中止
                    resolve('timeout');
                },5000);
            }

            let param_temp:string = "?";

            params['token'] = orc.orc_data.memory_data.token;
            for(var item in params){
                param_temp += item +"=" +params[item] +"&";
            }

            param_temp = param_temp.substr(0, param_temp.lastIndexOf('&'));

            //console.log('param:',params,param_temp);
           
            xhr.onreadystatechange = function () {

                //console.log('xhr.readyState='+xhr.readyState+'  xhr.status='+xhr.status);
                if (xhr.readyState === 4 && (xhr.status >= 200 && xhr.status < 300)) {
                    var respone = xhr.responseText;
                    if(loop == false)
                    {
                        if(time) return;//请求已经超时，忽略中止请求                    
                        clearTimeout(timer);//取消等待的超时  
                    }                         
                    //console.log("url:",url,respone);
                    resolve(respone);
                }               
            };
            
            let url_temp:string = '';
            if(orc.orc_config.DEBUG == true)
            {
                url_temp= orc.orc_config.HTTP_TEST_URL + url;
            }
            else
            {
                url_temp= orc.orc_config.HTTP_ONLINE_URL + url;
            }
            
            xhr.open("GET", url_temp + param_temp, true);

            xhr.send();
        })        
    }
}
